package com.spring.web.action.gf;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.spring.bean.gf.GfSort;
import com.spring.exception.ActionException;
import com.spring.service.gf.GfSortService;
import com.spring.utils.Constants;
import com.spring.utils.JsonUtils;
import com.spring.utils.StringUtils;
import com.spring.web.action.Action;
import com.spring.web.formbean.gf.GfSortForm;

/**
 * @author JingHaichao
 * 2012-11-19
 */
@Controller
public class GfSortAction extends Action {
	
	private final static Log log = LogFactory.getLog(GfSortAction.class);
	
	@Autowired
	private GfSortService gfSortService;
	
	/* query parent sorts */
	@RequestMapping(Constants.GF_SORT_ADMIN_PATH + "indexSort.do")
	public String queryForList(HttpServletRequest request) throws ActionException {
		try {
			List<GfSort> list = gfSortService.queryForList(Constants.RECORD_PID);
			request.setAttribute(Constants.GF_SORT_LIST_ATTR, list);
			return Constants.GF_SORT_ADMIN_PATH + "sort_index";
		} catch (Exception e) {
			log.debug("查询父级分类出错：" + e.getMessage(), e);
			throw new ActionException("查询父级分类出错：" + e.getMessage());
		}
	}
	
	/* query sub sorts*/
	@RequestMapping(Constants.GF_SORT_ADMIN_PATH + "getSortJson.do")
	public String queryForSubList(HttpServletRequest request) throws ActionException {
		try {
			Integer pid = paramsToForm(GfSortForm.class).getId();
			List<GfSort> list = gfSortService.queryForList(pid);
			request.setAttribute(Constants.GF_SORT_LIST_ATTR, list);
			return Constants.GF_SORT_ADMIN_PATH + "sub_sort_ajax";
		} catch (Exception e) {
			log.debug("查询子分类出错：" + e.getMessage(), e);
			throw new ActionException("查询子分类出错：" + e.getMessage());
		}
	}
	
	@RequestMapping(Constants.GF_SORT_ADMIN_PATH + "addSort.do")
	public String addSort() {
		return Constants.GF_SORT_ADMIN_PATH + "sort_add";
	}
	
	@RequestMapping(Constants.GF_SORT_ADMIN_PATH + "addSubSort.do")
	public String addSubSort(HttpServletRequest request) throws ActionException {
		try {
			GfSortForm form = paramsToForm(GfSortForm.class);
			Integer id = form.getId();
			GfSort sort = gfSortService.getById(id);
			request.setAttribute(Constants.GF_SORT_ATTR, sort);
			return Constants.GF_SORT_ADMIN_PATH + "sub_sort_add";
		} catch (Exception e) {
			throw new ActionException("");
		}
	}
	
	@RequestMapping(Constants.GF_SORT_ADMIN_PATH + "doAddSort.do")
	public String doAddSort() throws ActionException {
		try {
			GfSortForm form = paramsToForm(GfSortForm.class);
			gfSortService.insert(form);
			return "redirect:indexSort.do";
		} catch (Exception e) {
			log.debug("添加功夫分类出错：" + e.getMessage(), e);
			throw new ActionException("添加功夫分类出错：" + e.getMessage());
		}
	}
	
	@RequestMapping(Constants.GF_SORT_ADMIN_PATH + "editSort.do")
	public String editSort(HttpServletRequest request) throws ActionException {
		try {
			GfSortForm form = paramsToForm(GfSortForm.class);
			Integer id = form.getId();
			GfSort gfSort = gfSortService.getById(id);
			request.setAttribute(Constants.GF_SORT_ATTR, gfSort);
			return Constants.GF_SORT_ADMIN_PATH + "sort_edit";
		} catch (Exception e) {
			log.debug("编辑功夫分类出错：" + e.getMessage(), e);
			throw new ActionException("编辑功夫分类出错：" + e.getMessage());
		}
	}
	
	@RequestMapping(Constants.GF_SORT_ADMIN_PATH + "doEditSort.do")
	public String doEdit() throws ActionException {
		try {
			GfSortForm form = paramsToForm(GfSortForm.class);
			gfSortService.update(form);
			return "redirect:indexSort.do";
		} catch (Exception e) {
			log.debug("修改功夫分类出错：" + e.getMessage(), e);
			throw new ActionException("修改功夫分类出错：" + e.getMessage());
		}
	}
	
	@RequestMapping(Constants.GF_SORT_ADMIN_PATH + "deleteSort.do")
	public String delete() throws ActionException {
		try {
			GfSortForm form = paramsToForm(GfSortForm.class);
			Integer sortId = form.getId();
			Integer pId = form.getPid();
			if(sortId == null || pId == null) {
				throw new ActionException("传递参数出错");
			}
			gfSortService.delete(sortId, pId);
			return "redirect:indexSort.do";
		} catch (Exception e) {
			throw new ActionException("删除分类出错");
		}
	}
}
